/**
 * Created by lizhe on 2018/4/19.
 */
$(document).ready(function(){
    setTimeout(function(){
        if(maintainFlag == false){
            location.href = "maintenance.html";
        }
    },100);

    //if(getCookie("visit")=="visit"){
    //
    //}else{
    //    setCookieTime("visit","visit", 300);
    //    $.ajax({
    //        url : urladdr + "/website/statistics",
    //        method : "GET",
    //        success : function(data){
    //            if(data.code == 0){
    //                setCookieNow("count",data.count);
    //                setCookieNow("totalCount",data.totalCount);
    //            }else if(data.code == 404){
    //                location.href = "404.html"
    //            }else{
    //                modalinfo(data.msg);
    //            }
    //        }
    //    });
    //}
});

function openUrl(url){
    var str = url.split("://");
    if(str[0]=="http" || str[0]=="https"){
        window.open(url,'_blank');
    }else{
        var href = "http://" + url;
        window.open(href,'_blank');
    }
}

function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}

function returnPrev(){
    history.back(-1);
}

function queryList2(menuId){
    $.ajax({
        url : urladdr + "/web/menu/list",
        method : "GET",
        data : {
            menuId:menuId
        },
        success : function(data){
            if(data.code == 0){
                datas.splice(0,datas.length);
                first.splice(0,first.length);
                second.splice(0,second.length);
                third.splice(0,third.length);
                $("#menu").empty();
                $.each(data.data,function(i,date){
                    var d ={
                        id:date.id,name:date.title,parentId:date.parentId,height:date.height,type:date.type,icon:date.icon
                    };
                    datas.push(d);
                });
                for(var i =0;i<datas.length;i++){
                    if(datas[i].height==0){
                        var d = {
                            id:datas[i].id,name:datas[i].name,parentId:datas[i].parentId,height:datas[i].height,type:datas[i].type,icon:datas[i].icon
                        };
                        first.push(d);
                    }
                    if(datas[i].height==1){
                        d = {
                            id:datas[i].id,name:datas[i].name,parentId:datas[i].parentId,height:datas[i].height,type:datas[i].type,icon:datas[i].icon
                        };
                        second.push(d);
                    }
                    if(datas[i].height==2){
                        d = {
                            id:datas[i].id,name:datas[i].name,parentId:datas[i].parentId,height:datas[i].height,type:datas[i].type,icon:datas[i].icon
                        };
                        third.push(d);
                    }
                }

                queryNavigation(menuId)
                if(next!=0){
                    //自定拟部分栏目
                    document.getElementById("sidebar").style.overflowX = "hidden";
                    document.getElementById("menu").innerHTML = document.getElementById("menu").innerHTML +
                        '<li>'+
                        '<a href="index.html">'+
                        '<div class="columnMidLink"></div>'+
                        '<i class="fa"><img src="images/9902.png"></i>'+
                        '<span style="margin-left: 30px">校园首页</span>'+
                        '<div class="columnPoint"></div>'+
                        '</a>'+
                        '<div class="columnLink"></div>'+
                        '</li>';
                    $.each(second,function(i,date){
                        var x=0;
                        var content = '';
                        $.each(third,function(j,dateJ){
                            if(dateJ.type==1){
                                var href2 = "article.html?menuId="+dateJ.id+"&type="+dateJ.type+"&next=1";
                            }else if(dateJ.type == 2){
                                href2 = "album.html?menuId="+dateJ.id+"&type="+dateJ.type+"&next=1";
                            }else if(dateJ.type == 3){
                                href2 = "teacher.html?menuId="+dateJ.id+"&type="+dateJ.type+"&next=1";
                            }else if(dateJ.type == 4){
                                href2 = "student.html?menuId="+dateJ.id+"&type="+dateJ.type+"&next=1";
                            }else if(dateJ.type == 5){
                                var id = queryArticleId(dateJ.id);
                                href2 = "text.html?menuId="+dateJ.id+"&id="+id+"&next=1";
                            }else if(dateJ.type == 6){
                                href2 = "teacher2.html?menuId="+dateJ.id+"&type="+dateJ.type+"&next=1";
                            }
                            if(dateJ.parentId == date.id){
                                x=1;
                                if(menuId == dateJ.id){
                                    content = content + '<li><a href="'+href2+'" style="background-color:#3e8937;"><div class="columnMidLink" style="visibility: visible"></div>'+
                                        '<div class="column3Point"></div><div class="column3Text">'+dateJ.name+'</div>'+
                                        '<div class="columnPoint3" style="visibility: visible"></div></a>'+
                                        '<div class="column2Line"></div></li>'
                                }else{
                                    content = content + '<li><a href="'+href2+'"><div class="columnMidLink"></div>'+
                                        '<div class="column3Point"></div><div class="column3Text">'+dateJ.name+'</div>'+
                                        '<div class="columnPoint3"></div></a>'+
                                        '<div class="column2Line"></div></li>'
                                }
                            }
                        });

                        if(date.type==1){
                            var href = "article.html?menuId="+date.id+"&type="+date.type+"&next=1";
                        }else if(date.type == 2){
                            href = "album.html?menuId="+date.id+"&type="+date.type+"&next=1";
                        }else if(date.type == 3){
                            href = "teacher.html?menuId="+date.id+"&type="+date.type+"&next=1";
                        }else if(date.type == 4){
                            href = "student.html?menuId="+date.id+"&type="+date.type+"&next=1";
                        }else if(date.type == 5){
                            var id = queryArticleId(date.id);
                            href = "text.html?menuId="+date.id+"&id="+id+"&next=1";
                        }else if(date.type == 6){
                            href = "teacher2.html?menuId="+date.id+"&type="+date.type+"&next=1";
                        }
                        if(menuId==date.id){
                            if(content == '<li>'){
                                document.getElementById("menu").innerHTML = document.getElementById("menu").innerHTML +
                                    '<li>'+
                                    '<a href='+href+' style="background-color:#3e8937;">'+
                                    '<div class="columnMidLink" style="visibility: visible"></div>'+
                                    '<i class="fa"><img src="images/ygc_01%20copy.png" class="column2Img"></i>'+
                                    '<b class="hidden">'+date.id+'</b>'+
                                    '<span style="margin-left: 30px" class="column2Text">'+date.name+'</span>'+
                                    '<div class="columnPoint" style="visibility: visible"></div>'+
                                    '</a>'+
                                    '<div class="column2Line"></div>'+
                                    '</li>'
                            }else{
                                document.getElementById("menu").innerHTML = document.getElementById("menu").innerHTML +
                                    '<li>'+
                                    '<a href='+href+' style="background-color:#3e8937;">'+
                                    '<div class="columnMidLink" style="visibility: visible"></div>'+
                                    '<i class="fa"><img src="images/ygc_01%20copy.png" class="column2Img"></i>'+
                                    '<b class="hidden">'+date.id+'</b>'+
                                    '<span style="margin-left: 30px" class="column2Text">'+date.name+'</span>'+
                                    '<div class="columnPoint" style="visibility: visible"></div>'+
                                    '</a>'+
                                    '<div class="column2Line"></div>'+
                                    content+
                                    '</ul>' +
                                    '</li>'
                            }
                        }else{
                            if(content == '<li>'){
                                document.getElementById("menu").innerHTML = document.getElementById("menu").innerHTML +
                                    '<li>'+
                                    '<a href='+href+'>'+
                                    '<div class="columnMidLink"></div>'+
                                    '<i class="fa"><img src="images/ygc_01%20copy.png" class="column2Img"></i>'+
                                    '<b class="hidden">'+date.id+'</b>'+
                                    '<span style="margin-left: 30px" class="column2Text">'+date.name+'</span>'+
                                    '<div class="columnPoint"></div>'+
                                    '</a>'+
                                    '<div class="column2Line"></div>'+
                                    '</li>'
                            }else{
                                document.getElementById("menu").innerHTML = document.getElementById("menu").innerHTML +
                                    '<li>'+
                                    '<a href='+href+'>'+
                                    '<div class="columnMidLink"></div>'+
                                    '<i class="fa"><img src="images/ygc_01%20copy.png" class="column2Img"></i>'+
                                    '<b class="hidden">'+date.id+'</b>'+
                                    '<span style="margin-left: 30px" class="column2Text">'+date.name+'</span>'+
                                    '<div class="columnPoint"></div>'+
                                    '</a>'+
                                    '<div class="column2Line"></div>'+
                                    content+
                                    '</ul>' +
                                    '</li>'
                            }
                        }
                    });
                }else{
                    document.getElementById("menu").innerHTML = document.getElementById("menu").innerHTML +
                        '<li>'+
                        '<a href="index.html">'+
                        '<div class="columnMidLink"></div>'+
                        '<i class="fa"><img src="images/9902.png"></i>'+
                        '<span style="margin-left: 30px">校园首页</span>'+
                        '<div class="columnPoint"></div>'+
                        '</a>'+
                        '<div class="columnLink"></div>'+
                        '</li>';
                    //自定拟部分栏目
                    $.each(first,function(i,date){
                        var x=0;
                        var content = '<ul class="menu-academico-sub">';
                        $.each(second,function(j,dateJ){
                            if(dateJ.type==1){
                                var href2 = "article.html?menuId="+dateJ.id+"&type="+date.type+"&next=1";
                            }else if(dateJ.type == 2){
                                href2 = "album.html?menuId="+dateJ.id+"&type="+date.type+"&next=1";
                            }else if(dateJ.type == 3){
                                href2 = "teacher.html?menuId="+dateJ.id+"&type="+date.type+"&next=1";
                            }else if(dateJ.type == 4){
                                href2 = "student.html?menuId="+dateJ.id+"&type="+date.type+"&next=1";
                            }else if(dateJ.type == 5){
                                var id = queryArticleId(dateJ.id);
                                href2 = "text.html?menuId="+dateJ.id+"&id="+id+"&next=1";
                            }else if(dateJ.type == 6){
                                href2 = "teacher2.html?menuId="+dateJ.id+"&type="+date.type+"&next=1";
                            }
                            if(dateJ.parentId == date.id){
                                x=1;
                                if(menuId==date.id){
                                    content = content + '<li><b class="hidden">'+dateJ.id+'</b><a href='+href2+' style="background-color:#3e8937;>'+dateJ.name+'</a></li>'
                                }else{
                                    content = content + '<li><b class="hidden">'+dateJ.id+'</b><a href='+href2+'>'+dateJ.name+'</a></li>'
                                }
                            }
                        });
                        if(x==1){
                            var href = "#";
                        }else{
                            if(date.type==1){
                                href = "article.html?menuId="+date.id+"&type="+date.type+"&next=0";
                            }else if(date.type == 2){
                                href = "album.html?menuId="+date.id+"&type="+date.type+"&next=0";
                            }else if(date.type == 3){
                                href = "teacher.html?menuId="+date.id+"&type="+date.type+"&next=0";
                            }else if(date.type == 4){
                                href = "student.html?menuId="+date.id+"&type="+date.type+"&next=0";
                            }else if(date.type == 5){
                                var id = queryArticleId(date.id);
                                href = "text.html?menuId="+date.id+"&id="+id+"&next=0";
                            }else if(date.type == 6){
                                href = "teacher2.html?menuId="+date.id+"&type="+date.type+"&next=0";
                            }
                        }
                        if(menuId==date.id){
                            if(content == '<ul class="menu-academico-sub">'){
                                document.getElementById("menu").innerHTML = document.getElementById("menu").innerHTML +
                                    '<li>'+
                                    '<a href='+href+' style="background-color:#3e8937;">'+
                                    '<div class="columnMidLink" style="visibility: visible"></div>'+
                                    '<i class="fa"><img src="'+date.icon+'"></i>'+
                                    '<b class="hidden">'+date.id+'</b>'+
                                    '<span style="margin-left: 30px">'+date.name+'</span>'+
                                    '<div class="columnPoint" style="visibility: visible"></div>'+
                                    '</a>'+
                                    '<div class="columnLink"></div>'+
                                    '</li>'
                            }else{
                                document.getElementById("menu").innerHTML = document.getElementById("menu").innerHTML +
                                    '<li>'+
                                    '<a href='+href+' style="background-color:#3e8937;">'+
                                    '<div class="columnMidLink" style="visibility: visible"></div>'+
                                    '<i class="fa"><img src="'+date.icon+'"></i>'+
                                    '<b class="hidden">'+date.id+'</b>'+
                                    '<span style="margin-left: 30px">'+date.name+'</span>'+
                                    '<div class="columnPoint" style="visibility: visible"></div>'+
                                    '</a>'+
                                    '<div class="columnLink"></div>'+
                                    content+
                                    '</ul>' +
                                    '</li>'
                            }
                        }else{
                            if(content == '<ul class="menu-academico-sub">'){
                                document.getElementById("menu").innerHTML = document.getElementById("menu").innerHTML +
                                    '<li>'+
                                    '<a href='+href+'>'+
                                    '<div class="columnMidLink"></div>'+
                                    '<i class="fa"><img src="'+date.icon+'"></i>'+
                                    '<b class="hidden">'+date.id+'</b>'+
                                    '<span style="margin-left: 30px">'+date.name+'</span>'+
                                    '<div class="columnPoint"></div>'+
                                    '</a>'+
                                    '<div class="columnLink"></div>'+
                                    '</li>'
                            }else{
                                document.getElementById("menu").innerHTML = document.getElementById("menu").innerHTML +
                                    '<li>'+
                                    '<a href='+href+'>'+
                                    '<div class="columnMidLink"></div>'+
                                    '<i class="fa"><img src="'+date.icon+'"></i>'+
                                    '<b class="hidden">'+date.id+'</b>'+
                                    '<span style="margin-left: 30px">'+date.name+'</span>'+
                                    '<div class="columnPoint"></div>'+
                                    '</a>'+
                                    '<div class="columnLink"></div>'+
                                    content+
                                    '</ul>' +
                                    '</li>'
                            }
                        }
                    });
                    //链接栏目
                    document.getElementById("menu").innerHTML = document.getElementById("menu").innerHTML +
                        '<li>'+
                        '<a href="schoolLink.html">'+
                        '<div class="columnMidLink"></div>'+
                        '<i class="fa"><img src="images/9_06.png"></i>'+
                        '<span style="margin-left: 30px">学校链接</span>'+
                        '<div class="columnPoint"></div>'+
                        '</a>'+
                        '<div class="columnLink"></div>'+
                        '</li>'+
                        '<li>'+
                        '<a href="otherLink.html">'+
                        '<div class="columnMidLink"></div>'+
                        '<i class="fa"><img src="images/9908.png"></i>'+
                        '<span style="margin-left: 30px">其他链接</span>'+
                        '<div class="columnPoint"></div>'+
                        '</a>'+
                        '<div class="columnLink"></div>'+
                        '</li>'+
                        '<li>'
                }
            }else if(data.code == 401){
                location.href = "index.html"
            }else{
                modalinfo(data.msg);
            }
        }
    });
}


function queryNavigation(menuId){
    if(datas.length!=0){
        for(var i = 0;i<datas.length;i++){
            if(menuId == datas[i].id){
                if(datas[i].height == 0){
                    document.getElementById("navigation").innerHTML = "<a>"+ datas[i].name +"</a>"
                }else if(datas[i].height == 1){
                    var content = "<a onclick=location.href='index.html'>首页</a> > ";
                    document.getElementById("navigation").innerHTML = content + "<a>"+ datas[i].name +"</a>"
                }else if(datas[i].height == 2){
                    content = "<a onclick=location.href='index.html'>首页</a> > ";
                    for(var j = 0;j<datas.length;j++){
                        if(datas[i].parentId == datas[j].id){
                            content = content + "<a>" + datas[j].name + "</a> > ";
                            document.getElementById("navigation").innerHTML = content + "<a>"+ datas[i].name +"</a>";
                            break;
                        }
                    }
                }
                break;
            }
        }
        return false;
    }else{
        //queryNavigation(menuId);
    }
}

function aHover(obj){
    obj.style.background = "#4A9641";
}

function aMove(obj){
    obj.style.background = "#1B4A2B";
}

function setCookieTime(name, value,s){
    var m = s*1000;
    var exp = new Date();
    exp.setTime(exp.getTime() + m);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}

function setCookieNow(name, value){
    var cookies = document.cookie.split(";");
    var isContainsName = false;
    for(var index = 0; index < cookies.length; index++){
        var cookie = cookies[index];
        if (cookie.trim() === ""){
            cookies.splice(index, 1);
            index--;
        }else {
            cookie = cookie.split("=");
            var cookieName = cookie[0].trim();
            if (cookieName === name.trim()){
                cookie = name + "=" + value;
                document.cookie = cookie;
                isContainsName = true;
                return;
            }
        }
    }
    if (isContainsName === false){
        // 没有找到对应的key，直接添加相应的cookie
        var cookie = name + "=" + value;
        document.cookie = cookie;
    }
}

function indexFlash(){
    delCookie("flag");
    location.href = "index.html";
}

function queryArticleId(menuId){
    var id = 0;
    $.ajax({
        url : urladdr + "/web/article/list",
        method : "GET",
        data : {
            page:1,
            pageSize:1,
            menuId:menuId,
            status:2
        },
        async : false,
        success : function(data){
            if(data.code == 0){
                if(data.data.list.length!=0){
                    id = data.data.list[0].id;
                }
            }else if(data.code == 401){
                location.href = "index.html"
            }else{
                modalinfo(data.msg);
            }
        }
    });
    return id;
}